package com.msb.bf;

import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;

/**
 * @author: zhaoyi
 * @create: 2020-05-08 12:45
 * @description: 软引用示例
 **/

public class SoftReferenceCache {

    //10M
    private static SoftReference<byte[]> data = new SoftReference<>(new byte[1024 * 1024 * 10]);

    //-Xmx15m 执行参数
    public static void main(String[] args) throws InterruptedException {
        System.out.println(data.get()); //[B@6842775d
        System.gc();
        TimeUnit.SECONDS.sleep(10);
        System.out.println(data.get()); //[B@6842775d

        byte[] bytes = new byte[1024 * 1024 * 1];
        System.out.println(data.get()); //[B@6842775d

        bytes = new byte[1024 * 1024 * 4];
        System.out.println(data.get()); //null


    }


}
